package com.example.celldian.core;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;

import com.example.celldian.db.DBCharacter;
import com.example.celldian.db.DBInventory;

public class Character {

	private String _charName;
	private int _level;
	private String _classType;
	private String _type;
	private int _xp;
	private int _hp;
	private int _mp;
	private int _stamina;
	private int _intelect;
	private int _strength;
	private int _agility;
	private int _karma;
	private String _mapName;
	private double _posx;
	private double _posy;
	
	private HashMap<String, Item> _itemList;
	private HashMap<String, Skill> _skillList;
	
	public Character() {	
	}
	
	public void loadCharacter(String charname, Context activity) {
		
		DBCharacter char_db = new DBCharacter(activity);
		
		HashMap<String, String> character = char_db.getCharacter(charname);
		
		if (!character.isEmpty()) {
			_charName = character.get(Constants.CHAR_NAME);
			_level = Integer.parseInt(character.get(Constants.CHAR_LVL));
			_classType = character.get(Constants.CHAR_CLASS);
			_type = character.get(Constants.CHAR_TYPE);
			_xp = Integer.parseInt(character.get(Constants.CHAR_XP));
			_hp = Integer.parseInt(character.get(Constants.CHAR_HP));
			_mp = Integer.parseInt(character.get(Constants.CHAR_MP));
			_stamina = Integer.parseInt(character.get(Constants.CHAR_STAMINA));
			_intelect = Integer.parseInt(character.get(Constants.CHAR_INTELECT));
			_strength = Integer.parseInt(character.get(Constants.CHAR_STRENGTH));
			_agility = Integer.parseInt(character.get(Constants.CHAR_AGILITY));
			_karma = Integer.parseInt(character.get(Constants.CHAR_KARMA));
			_mapName = character.get(Constants.CHAR_MAP);
			_posx = Double.parseDouble(character.get(Constants.CHAR_POSX));
			_posy = Double.parseDouble(character.get(Constants.CHAR_POSY));
		}
		
		loadInventory(activity);
	}
	
	public void saveCharacter(Context activity) {
		DBCharacter char_db = new DBCharacter(activity);
		
		HashMap<String, String> character = new HashMap<String, String>();
		
		character.put(Constants.CHAR_NAME, _charName);
		character.put(Constants.CHAR_LVL, toString(_level));
		character.put(Constants.CHAR_CLASS, _classType);
		character.put(Constants.CHAR_TYPE, _type);
		character.put(Constants.CHAR_XP, toString(_xp));
		character.put(Constants.CHAR_HP, toString(_hp));
		character.put(Constants.CHAR_MP, toString(_mp));
		character.put(Constants.CHAR_STAMINA, toString(_stamina));
		character.put(Constants.CHAR_INTELECT, toString(_intelect));
		character.put(Constants.CHAR_STRENGTH, toString(_strength));
		character.put(Constants.CHAR_AGILITY, toString(_agility));
		character.put(Constants.CHAR_KARMA, toString(_karma));
		character.put(Constants.CHAR_MAP, _mapName);
		character.put(Constants.CHAR_POSX, toString(_posx));
		character.put(Constants.CHAR_POSY, toString(_posy));
		
		if (char_db.getCharacter(_charName).isEmpty()) {
			char_db.insertCharacter(character);
		} 
		else {
			char_db.updateCharacter(character);
		}
	}
	
	public void loadInventory(Context activity) {
		DBInventory inventory_db = new DBInventory(activity);
		
		ArrayList<String> inventory = inventory_db.getInventory(_charName);
		
		if (!inventory.isEmpty()) {
			for (int i = 0; i < inventory.size(); i += 4) {
				String itemName = inventory.get(i);
				int itemLevel = Integer.parseInt(inventory.get(i + 1));
				String type = inventory.get(i + 2);
				String description = inventory.get(i + 3);
				
				Item item = new Item(itemName, itemLevel, type, description);
				
				_itemList.put(itemName, item);
			}
		}
	}
	
	public void saveInventory(Context activity) {
		DBInventory inventory_db = new DBInventory(activity);
		
		if (inventory_db.getInventory(_charName).isEmpty()) {
			for (Item item : _itemList.values()) {
				HashMap<String, String> itemValues = new HashMap<String, String>();
				itemValues.put("charname", _charName);
				itemValues.put("itemname", item.getItemName());
				itemValues.put("itemlevel", toString(item.getItemLevel()));
				itemValues.put("type", item.getType());
				itemValues.put("description", item.getDescription());
				
				inventory_db.insertInventoryValue(itemValues);
			}
		} else {
			for (Item item : _itemList.values()) {
				HashMap<String, String> itemValues = new HashMap<String, String>();
				itemValues.put("charname", _charName);
				itemValues.put("itemname", item.getItemName());
				itemValues.put("itemlevel", toString(item.getItemLevel()));
				itemValues.put("type", item.getType());
				itemValues.put("description", item.getDescription());
				
				inventory_db.updateInventoryValue(itemValues);
			}
		}
	}
	
	public String toString(Number num) {
		return num + "";
	}
	
	//-----------SETTERS-----------
	
	public void setCharName(String charname) {
		_charName = charname;
	}
	
	public void setLevel(int level) {
		_level = level;
	}
	
	public void setClassType(String classtype) {
		_classType = classtype;
	}
	
	public void setType(String type) {
		_type = type;
	}
	
	public void setXp(int xp) {
		_xp = xp;
	}
	
	public void setHp(int hp) {
		_hp = hp;
	}
	
	public void setMp(int mp) {
		_mp = mp;
	}
	
	public void setStamina(int stamina) {
		_stamina = stamina;
	}
	
	public void setIntelect(int intelect) {
		_intelect = intelect;
	}
	
	public void setStrength(int strength) {
		_strength = strength;
	}
	
	public void setAgility(int agility) {
		_agility = agility; 
	}
	
	public void setkarma(int karma) {
		_karma = karma;
	}
	
	public void setMapName(String mapname) {
		_mapName = mapname;
	}
	
	public void setPosX(double posx) {
		_posx = posx;
	}
	
	public void setPosY(double posy) {
		_posy = posy;
	}
	
	public void setItems(HashMap<String, Item> itemList) {
		_itemList = itemList;
	}
	
	public void setItem(String key, Item item) {
		_itemList.put(key, item);
	}
	
	public void setSkills(HashMap<String, Skill> skillList) {
		_skillList = skillList;
	}
	
	public void setSkill(String key, Skill skill) {
		_skillList.put(key, skill);
	}
	
	//-----------GETTERS-----------
	
	public String getCharName() {
		return _charName;
	}
	
	public int getLevel() {
		return _level;
	}
	
	public String getClassType() {
		return _classType;
	}
	
	public String getType() {
		return _type;
	}
	
	public int getXp() {
		return _xp;
	}
	
	public int getHp() {
		return _hp;
	}
	
	public int getMp() {
		return _mp;
	}
	
	public int getStamina() {
		return _stamina;
	}
	
	public int getIntelect() {
		return _intelect;
	}
	
	public int getStrength() {
		return _strength;
	}
	
	public int getAgility() {
		return _agility; 
	}
	
	public int getkarma() {
		return _karma;
	}
	
	public String getMapName() {
		return _mapName;
	}
	
	public double getPosX() {
		return _posx;
	}
	
	public double getPosY() {
		return _posy;
	}
	
	public HashMap<String, Item> getItems() {
		return _itemList;
	}
	
	public Item getItem(String key) {
		return _itemList.get(key);
	}
	
	public HashMap<String, Skill> getSkills() {
		return _skillList;
	}
	
	public Skill getSkill(String key) {
		return _skillList.get(key);
	}
}
